package testappclient;

import guiclient.Application;
import guicommon.controls.*;
import guicommon.controls.handlers.*;
import java.awt.Color;
import java.io.IOException;

public class ClientGUI extends Application {

    private Window window;
    private Button sumButton;
    private Button closeButton;
    private Label realNumberLabel;
    private Label intNumberLabel;
    private TextBox tbLastNumber;
    private TextBox tbSum;
    private double sum = 0;

    ClientGUI(String number) throws IOException, InterruptedException {
        int num = Integer.valueOf(number);
        window = new Window();
        window.SetSize(200, 150);
        window.SetPoint(50 + 210 * (num % 2), 10 + 160 * (num == 1 ? 0 : 1));
        window.SetTitle("Client " + number);
        window.SetBackGround(Color.lightGray);
        RegisterControl(window);

        realNumberLabel = new Label();
        realNumberLabel.SetSize(50, 20);
        realNumberLabel.SetPoint(15, 30);
        realNumberLabel.SetOwner(window);
        realNumberLabel.SetText("Last:");
        realNumberLabel.SetBackGround(Color.lightGray);
        RegisterControl(realNumberLabel);

        intNumberLabel = new Label();
        intNumberLabel.SetSize(50, 20);
        intNumberLabel.SetPoint(15, 60);
        intNumberLabel.SetOwner(window);
        intNumberLabel.SetText("Sum:");
        intNumberLabel.SetBackGround(Color.lightGray);
        RegisterControl(intNumberLabel);

        tbLastNumber = new TextBox();
        tbLastNumber.SetSize(100, 20);
        tbLastNumber.SetPoint(75, 30);
        tbLastNumber.SetOwner(window);
        tbLastNumber.SetText("0");
        tbLastNumber.SetBackGround(Color.white);
        RegisterControl(tbLastNumber);

        tbSum = new TextBox();
        tbSum.SetSize(100, 20);
        tbSum.SetPoint(75, 60);
        tbSum.SetOwner(window);
        tbSum.SetText("0");
        tbSum.SetBackGround(Color.white);
        RegisterControl(tbSum);

        sumButton = new Button();
        sumButton.SetSize(70, 20);
        sumButton.SetPoint(25, 90);
        sumButton.SetOwner(window);
        sumButton.SetBackGround(Color.gray);
        sumButton.SetText("Sum");
        sumButton.SetMouseClickedEventHandler(new MouseClickedEventHandler() {

            @Override
            public void Event(Control sender, MouseEventArgs args) {
                tbSum.SetText(String.valueOf(sum));
            }
        });
        RegisterControl(sumButton);
    }

    void AddNumber(double num) {
        sum += num;
        tbLastNumber.SetText(String.valueOf(num));
    }

    void AddCloseButton() {
        closeButton = new Button();
        closeButton.SetSize(70, 20);
        closeButton.SetPoint(100, 90);
        closeButton.SetOwner(window);
        closeButton.SetBackGround(Color.red);
        closeButton.SetText("Close");
        closeButton.SetMouseClickedEventHandler(new MouseClickedEventHandler() {

            @Override
            public void Event(Control sender, MouseEventArgs args) {
                Close();
            }
        });
        RegisterControl(closeButton);
    }

}
